package cn.zy.amyan.server;

import cn.zy.amyan.server.handler.ServerInitializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.util.logging.Logger;


/**
 * @author 子言
 * @date 2024/5/5 下午 16:25
 */
public class AyServer {
	private static final Logger log = Logger.getLogger("appLogger");

	public static void start(String host, int port) {
		doStart(host, port);
	}

	private static void doStart(String host, int port) {
		EventLoopGroup boss = new NioEventLoopGroup(1);
		EventLoopGroup worker = new NioEventLoopGroup();

		try {
			ServerBootstrap server = new ServerBootstrap();
			server.group(boss, worker)
					.channel(NioServerSocketChannel.class)
					.childHandler(new ServerInitializer());

			ChannelFuture future = server.bind(host, port).sync();
			log.info("服务器启动完成，地址为：" + host + ":" + port);
			future.channel().closeFuture().sync();
		} catch (InterruptedException e) {
			log.severe("服务器启动失败！" + e);
		} finally {
			boss.shutdownGracefully();
			worker.shutdownGracefully();
		}
	}
}
